#include <bits/stdc++.h>
// 2025/06/11
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;

const int N = 101;
int n, q, f[N][N], k[N];
vector<pair<int, int>> edge[N];

void help(int u, int fa){
    for (auto [v, w] : edge[u]){
        if (v == fa) continue;
        k[v] = w;
        help(v, u);
    }
}

void dfs(int u, int fa){
    for (auto [v, w] : edge[u]){
        if (v == fa) continue;
        dfs(v, u);
        for (int i = q; i >= 1; i--)
            for (int j = 1; j <= i; j++)
                f[u][i] = max(f[u][i], f[u][i - j] + f[v][j - 1] + k[v]);
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i < n; i++){
        int u, v, w;
        cin >> u >> v >> w;
        edge[u].push_back({v, w});
        edge[v].push_back({u, w});
    }
    help(1, 0);
    dfs(1, 0);
    cout << f[1][q] << endl;
    return 0;
}